home *** CD-ROM | disk | FTP | other *** search
- /*
- ** File: Networking.c
- **
- ** Written by: Bill Hayden
- ** Nikol Software
- **
- ** Based on: AppleTalk.c and AEUtils.c
- ** by Pete Helme, Jim Luther, and Eric Soldan of Apple Computer, Inc.
- **
- ** Copyright © 1995 Nikol Software
- ** All rights reserved.
- **
- */
-
-
-
-
- /*****************************************************************************/
-
-
-
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __OSUTILS__
- #include <OSUtils.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #include "StringUtils.h"
- #include "Networking.h"
-
-
- #define kATPTimeOutVal 3 /* re-try ATP SendRequest every 3 seconds */
- #define kATPRetryCount 5 /* for five times */
- #define kZonesSize 578 /* size of buffer for zone names */
- #define kGetMyZoneCall 0x07000000 /* GetMyZone indicator */
- #define kGetZoneListCall 0x08000000 /* GetZoneList indicator */
- #define kZIPSocket 6 /* the Zone Information Protocol socket */
- #define kMoreZones 0xFF000000 /* mask to see if more zones to come */
- #define kZoneCount 0x0000FFFF /* mask to count zones in buffer */
-
- #define kTupleSize 104
- #define kMaxTuples 1
-
-
-
- /*****************************************************************************/
-
-
-
-
-
- /* Create the list of zones on the network. Find a bridge to talk to, if one is
- ** present, then ask it for zone names. Add the names to the passed-in list. */
-
- OSErr DoBuildZoneList(ListHandle listHndl)
- {
- #if 0
- ATPParamBlock atppb;
- char zones[kZonesSize], *zptr, data[255];
- OSErr err;
-
- BDSElement dBDS; /* the BDS for GetZoneList call */
- short index, count, i;
- short ignore;
- short nodeNetAddress, bridgeNode;
-
- dBDS.buffSize = kZonesSize; /* set up BDS */
- dBDS.buffPtr = zones;
-
- atppb.ATPatpFlags = 0;
-
- /* Get network address of node & node ID of bridge (if any). */
-
- err = GetNodeAddress(&ignore, &nodeNetAddress);
- if (err) return(err);
-
- if (!(bridgeNode = GetBridgeAddress())) return(noErr);
- /* We have added all zero zones to the list, so we are done. */
-
- atppb.ATPaddrBlock.aNet = nodeNetAddress;
- atppb.ATPaddrBlock.aNode = bridgeNode; /* Get node of bridge. */
- atppb.ATPaddrBlock.aSocket = kZIPSocket; /* The socket we want. */
- atppb.ATPreqLength = 0;
- atppb.ATPreqPointer = nil;
- atppb.ATPbdsPointer = (Ptr) &dBDS;
- atppb.ATPnumOfBuffs = 1;
- atppb.ATPtimeOutVal = kATPTimeOutVal;
- atppb.ATPretryCount = kATPRetryCount;
-
- index = 1;
- count = 0;
-
- do {
- atppb.ATPuserData = kGetZoneListCall + index; /* Indicate GetZoneList request. */
- err = PSendRequest(&atppb, false); /* Send sync request. */
- if (err) return(err);
-
- count += dBDS.userBytes & kZoneCount; /* find out how many returned */
- zptr = zones; /* put current pointer at start */
- do { /* get each zone */
- for (i = zptr[0]; i; --i)
- data[i - 1] = zptr[i];
- CLInsert(listHndl, data, zptr[0], -1, 0);
- zptr += (*zptr + 1 ); /* bump up current pointer*/
- ++index; /* increment which zone */
- } while(index <= count);
-
- } while ((dBDS.userBytes & kMoreZones) == 0); /* keep going until none left */
-
- #endif
- return(noErr);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* Select our zone in the zone list. */
-
- OSErr OldStyleGetMyZone(StringPtr str);
- OSErr HiliteUserZone(ListHandle listHndl)
- {
- Str32 zone;
- short zoneLen, i;
- Point cell;
- OSErr err;
-
- if (!(err = OldStyleGetMyZone(zone)))
- {
- for (zoneLen = zone[i = 0]; i < zoneLen; ++i) zone[i] = zone[i + 1];
- cell.h = cell.v = 0;
- if (LSearch(zone, zoneLen, nil, &cell, listHndl))
- {
- LSetSelect(true, cell, listHndl);
- LAutoScroll(listHndl);
- }
- }
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- OSErr OldStyleGetMyZone(StringPtr str)
- {
- ATPParamBlock atppb;
- OSErr err;
-
- BDSElement dBDS; /* the BDS for GetZoneList call */
- short ignore;
- short nodeNetAddress, bridgeNode;
-
- dBDS.buffSize = sizeof(Str32);
- dBDS.buffPtr = (Ptr)str;
-
- atppb.ATPatpFlags = 0;
-
- /* Get network address of node & node ID of bridge (if any). */
-
- err = GetNodeAddress(&ignore, &nodeNetAddress);
- if (err) return(err);
-
- if (!(bridgeNode = GetBridgeAddress())) return(noErr);
- /* We have added all zero zones to the ist, so we are done. */
-
- atppb.ATPaddrBlock.aNet = nodeNetAddress;
- atppb.ATPaddrBlock.aNode = bridgeNode; /* Get node of bridge. */
- atppb.ATPaddrBlock.aSocket = kZIPSocket; /* The socket we want. */
- atppb.ATPreqLength = 0;
- atppb.ATPreqPointer = nil;
- atppb.ATPbdsPointer = (Ptr) &dBDS;
- atppb.ATPnumOfBuffs = 1;
- atppb.ATPtimeOutVal = kATPTimeOutVal;
- atppb.ATPretryCount = kATPRetryCount;
-
- atppb.ATPuserData = kGetMyZoneCall; /* Indicate GetMyZone request. */
- return(PSendRequest(&atppb, false)); /* Send sync request. */
- }
-
-
-
-
- /*****************************************************************************/
-
-
-
- /* This routine finds the socket used by the PPC Toolbox (it's the one with
- ** type 'PPCToolBox') and gives it a NBP alias (it registers a new NBP name
- ** on that socket) with the type passed in newNBPType. The NameTableEntry
- ** record passed to this routine must be allocated globally (or must be a
- ** locked block on the heap until RemoveNBPAlias is called). The variable
- ** newEntity is filled in with the new entity name and is returned to the
- ** function's caller so it can be passed to the RemoveNBPAlias function
- ** (below). You'll get an error if any call fails, if an entity of type
- ** 'PPCToolBox' is not found (usually because either Program Linking isn't
- ** enabled or AppleTalk is disabled), or if RegisterName fails because >100
- ** copies of your application are running on the one machine (yea, right). */
-
- OSErr AddPPCNBPAlias(NamesTableEntry *theNTE, Str32 newNBPType, EntityName *newEntity)
- {
- OSErr err;
- MPPParamBlock pb;
- char keepSelfFlag;
- short keepResFile, origLen, num, len;
-
- EntityName myEntityName;
-
- AddrBlock myAddrBlock;
- char myRetBuff[kTupleSize];
- Handle machineNameHndl;
- Str32 machineName;
-
- pb.SETSELF.newSelfFlag = 1;
- err = PSetSelfSend(&pb, false); /* Turn on self-send. */
- if (err) return(err);
-
- keepSelfFlag = pb.SETSELF.oldSelfFlag; /* Keep old self-send flag. */
-
- keepResFile = CurResFile();
- UseResFile(0);
- machineNameHndl = GetResource('STR ', -16413); /* Get machine name. */
- UseResFile(keepResFile);
-
- if (!machineNameHndl)
- {
- pb.SETSELF.newSelfFlag = keepSelfFlag;
- PSetSelfSend(&pb, false);
- return(ResError());
- }
-
- pcpy(machineName, (StringPtr)*machineNameHndl); /* Keep a copy of the machine name. */
- ReleaseResource(machineNameHndl); /* Release the machine name resource. */
-
- NBPSetEntity((Ptr)&myEntityName, (StringPtr)machineName, (StringPtr)"\pPPCToolBox", (StringPtr)"\p*");
-
- pb.NBPinterval = 1; /* We want to build the entity name using */
- pb.NBPcount = 1; /* the machine name and 'PPCToolBox'. */
- pb.NBPentityPtr = (Ptr)&myEntityName;
- pb.NBPretBuffPtr = myRetBuff;
- pb.NBPretBuffSize = (kTupleSize * kMaxTuples);
- pb.NBPmaxToGet = kMaxTuples;
-
- if (!(err = PLookupName(&pb, false)))
- { /* If lookup was okay... */
- if (pb.NBPnumGotten)
- { /* If entity was found... */
- /* ...we found the socket used by the PPC Toolbox. */
- /* This means that there is a socket, due to program */
- /* linking being turned on. */
-
- NBPExtract(myRetBuff, pb.NBPnumGotten, 1, &myEntityName, &myAddrBlock);
- /* Break the tuple into component parts. */
-
- for (origLen = machineName[num = 0]; num < 100;)
- {
- pb.NBPinterval = 7;
- pb.NBPcount = 5;
- pb.NBPentityPtr = (Ptr)theNTE;
- pb.NBPverifyFlag = 1;
-
- NBPSetNTE((Ptr)theNTE, (StringPtr)machineName, (StringPtr)newNBPType, (StringPtr)"\p*", myAddrBlock.aSocket);
- NBPSetEntity((Ptr)newEntity, (StringPtr)machineName,(StringPtr) newNBPType, (StringPtr)"\p*");
- err = PRegisterName(&pb, false);
-
- if (err != nbpDuplicate) break;
- /* We registered the name, (or badness happened), so we are done. */
-
- len = 31; /* The name we tried already exists, so make up an alternate. */
- if (++num > 9)
- --len;
- if (origLen > len)
- origLen = len;
- machineName[0] = origLen;
- pcatdec(machineName, num);
- }
- }
- }
-
- pb.SETSELF.newSelfFlag = keepSelfFlag;
- PSetSelfSend(&pb, false);
-
- return(err);
- }
-
-
-
-
- /*****************************************************************************/
-
-
-
- /* This function removes the entity specified by theEntity from the registered
- ** names queue. You'll get an error if theEntity hasn't been registered on
- ** this Macintosh with RegisterName. */
-
- OSErr RemoveNBPAlias(EntityPtr theEntity)
- {
- MPPParamBlock pb;
-
- pb.MPPioCompletion = nil;
- pb.NBPentityPtr = (Ptr)theEntity;
- return(PRemoveName(&pb, false));
- }
-
-
-
-
- /*****************************************************************************/
-
-
-
-
- OSErr MakeTarget(AEAddressDesc *target, short replyMode,
- Str255 prompt, Str255 applListLabel,
- PPCFilterUPP portFilter, char *theLocNBPType)
- {
- OSErr err;
- TargetID theTargetID;
-
- static LocationNameRec location;
- static PortInfoRec portInfo;
- static Boolean defaultOK = false;
-
-
- target->dataHandle = nil;
- /* Assume we will fail and nil this descriptor out. */
-
- err = PPCBrowser( prompt, /* Browse dialog box prompt. */
- applListLabel, /* The 'programs' list title. */
- defaultOK, /* Initially false. */
- &location, /* Correct if defaultOK is true. */
- &portInfo, /* Correct if defaultOK is true. */
- portFilter, /* Port filtering. */
- (StringPtr)theLocNBPType /* List ports of this type */
- );
-
- if (!err)
- {
- defaultOK = true;
-
- theTargetID.location = location;
- theTargetID.name = portInfo.name;
- /* The fields sessionID does not need to be filled in now.
- ** The sessionID is returned when you actually connect to
- ** a port. You can then use the sessionID from that point
- ** on to improve speed.
- **
- ** You also don't need to fill in the recvrName field at this
- ** point. This is filled in, again, when the session is
- ** actually established.
- **
- ** We need the whole dealie for our target, since
- ** it is out on the net somewhere. */
-
- err = AECreateDesc( typeTargetID, /* Standard target descriptor type. */
- (Ptr)&theTargetID, /* The data for the descriptor. */
- sizeof(theTargetID), /* Size of the data. */
- target ); /* Wherefore art thou desc. */
- }
-
- return (err);
- }
-